module.exports = function(config){

  let totalCount = config.totalCount; // 总的数据条目数
  let pageSize   = config.pageSize || 2; // 单页条数
  let nowPage = config.nowPage || 1; // 目前显示第几页
  let complete = config.complete || function(){};
  // alert(nowPage)

  _page(nowPage);

  function _page(nowPage){
    $('.page-box .page').html('');

    let pageNum = Math.ceil(totalCount / pageSize);

    let pageHtml = '';
    pageHtml = '<a href="javascript:;" class="prevP" >&lt;</a>';

    if( pageNum > 6 ){

      pageHtml += `<a href="javascript:;" >${1}</a>`;
      // nowPage
      let p1 = nowPage - 2, p2 = nowPage - 1, p3 = nowPage, p4 = nowPage + 1;

      if( nowPage <= 5 ){

        for( let i = 2; i <= 5; i ++ ){
          if( i == nowPage ){
            pageHtml += `<a href="javascript:;" class="active" >${i}</a>`;
          }else{
            pageHtml += `<a href="javascript:;">${i}</a>`;
          }
        }

        pageHtml += `<a href="javascript:;" class="nouse" >...</a>`;

      }else if( nowPage + 5 > pageNum ){

        pageHtml += `<a href="javascript:;" class="nouse" >...</a>`;

        for( let i = pageNum - 4; i <= pageNum; i ++ ){
          if( i == nowPage ){
            pageHtml += `<a href="javascript:;" class="active" >${i}</a>`;
          }else{
            pageHtml += `<a href="javascript:;">${i}</a>`;
          }
        }

      }else{

        pageHtml += `<a href="javascript:;" class="nouse" >...</a>`;

        pageHtml += `<a href="javascript:;">${p1}</a>`;
        pageHtml += `<a href="javascript:;">${p2}</a>`;
        pageHtml += `<a href="javascript:;" class="active" >${p3}</a>`;
        pageHtml += `<a href="javascript:;">${p4}</a>`;

        pageHtml += `<a href="javascript:;" class="nouse" >...</a>`;
      }


      pageHtml += `<a href="javascript:;">${pageNum}</a>`;


    }else{

      for( let i = 0; i < pageNum; i ++ ){
        if( nowPage == i+1 ){
          // debugger;
          pageHtml += '<a href="javascript:;" class="active" >'+ (i+1) +'</a>';
        }else {
          pageHtml += '<a href="javascript:;">'+ (i+1) +'</a>';
        }

      }

    }
    pageHtml += '<a href="javascript:;" class="nextP" >&gt;</a>';
    $('.page-box .page *').off();
    $('.page-box .page').html(pageHtml);

    $('.page-box .page a').click( function(){
      if( $(this).hasClass('nouse') ){

      }else if( $(this).hasClass('active') ){

      }else if( $(this).hasClass('prevP') ) {

        if( nowPage != 1 ){
          complete(nowPage - 1);
          _page(nowPage - 1);

        }

      }else if( $(this).hasClass('nextP') ){

        if( nowPage != pageNum ){
          complete(nowPage + 1);
          _page(nowPage + 1);

        }

      }else{
        // console.log($(this).text())
        complete($(this).text() - 0);
        _page( $(this).text() - 0 );


      }

    } );

  }

};
